<template>
  <div class="app-container">
    <div class="head_search u-f u-f-ac u-fw">
      <div class="ipt_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("订单ID") }}：</p>
        <el-input v-model="order_id" :placeholder="$t('请输入订单ID')"></el-input>
      </div>
      <div class="ipt_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("会员号") }}：</p>
        <el-input v-model="member_id" :placeholder="$t('请输入会员号')"></el-input>
      </div>
      <div class="ipt_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("二维码") }}：</p>
        <el-input v-model="qrcode" :placeholder="$t('请输入二维码')"></el-input>
      </div>
      <div class="select_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("客户端") }}：</p>
        <el-select v-model="clientVal" :placeholder="$t('请选择')" @change="clientChange">
          <el-option
            v-for="item in clientArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="select_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("订单状态") }}：</p>
        <el-select v-model="orderStatusVal" :placeholder="$t('请选择')" @change="orderStatusChange">
          <el-option
            v-for="item in orderStatusArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="div_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("所属代理商") }}：</p>
        <div class="content font14 text_black u-f u-f-ac" @click="openAgentList">
          <p class="text-ellipsis">{{ agent ? agent : $t("选择代理商") }}</p>
        </div>
        <button class="btn font14 text_white bg_danger" @click="clearAgent">{{ $t("清除") }}</button>
      </div>
      <div class="div_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("所属商户") }}：</p>
        <div class="content font14 text_black u-f u-f-ac" @click="openMerchantList">
          <p class="text-ellipsis">{{ merchant ? merchant : $t("选择商户") }}</p>
        </div>
        <button class="btn font14 text_white bg_danger" @click="clearMerchant">{{ $t("清除") }}</button>
      </div>
      <div class="date_box u-f u-f-ac">
        <p class="label font14 text_black">{{ $t("下单时间") }}：</p>
        <el-date-picker
          v-model="dates"
          type="datetimerange"
          :picker-options="pickerOptions"
          :range-separator="$t('至')"
          :start-placeholder="$t('开始日期')"
          :end-placeholder="$t('结束日期')"
          align="right"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
        <p class="tip font14 text_danger">{{$t('只能选择一个月的范围')}}</p>
      </div>
      <button class="sub_btn font14 text_white bg_blue" @click="getData('search')">{{ $t("提交") }}</button>
    </div>
    <p class="head_tip font14 text_danger">
      {{ $t("72小时内的订单可以撤销") }},{{ $t("撤销订单后") }},
      {{ $t("订单金额会退回用户余额") }},{{ $t("所有代理佣金也将撤销") }}
    </p>

    <div>
      <el-table :data="tableData">
        <el-table-column prop="order_no" :label="$t('订单ID')" width="170"></el-table-column>
        <el-table-column prop="member_id" :label="$t('会员号')" width="90"></el-table-column>
        <el-table-column prop="start_time" :label="$t('使用时间')" width="160">
          <template slot-scope="{ row }">
            <span>{{ row.start_time }} {{ row.end_time }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="amount" :label="$t('金额')"></el-table-column>
        <el-table-column prop="status_text" :label="$t('订单状态')"></el-table-column>
        <el-table-column prop="nick" :label="$t('用户名称')"></el-table-column>
        <el-table-column prop="billing_unit" :label="$t('购买时长')"></el-table-column>
        <el-table-column prop="app_name" :label="$t('客户端')"></el-table-column>
        <el-table-column prop="seller_name" :label="$t('商户名称')"></el-table-column>
        <el-table-column prop="agency_name" :label="$t('所属代理')"></el-table-column>
        <el-table-column prop="device_code" :label="$t('二维码')" width="130"></el-table-column>
        
        <el-table-column :label="$t('操作')" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-if="permissions.indexOf('/order/detail') != -1"
              size="mini"
              type="success"
              @click="handleDetail(scope.row)"
            >{{ $t("详情") }}</el-button>
            <el-button
              v-if="(permissions.indexOf('/order/cancelOrder') != -1) && scope.row.is_cancel"
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
            >{{ $t("撤销") }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination_box">
        <el-pagination
          background
          
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-size="page_size"
          layout="total, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>

    <!-- 订单详情和修改 -->
    <el-dialog
      :title="$t('详情')"
      width="900px"
      :visible.sync="showOrderDetail"
      :close-on-click-modal="false"
    >
      <div class="order_detail">
        <div class="txt_list">
          <span class="lable font14 text_black">{{$t('订单号')}}：</span>
          <span class="txt font14 text_black">{{ detailFrom.order_no }}</span>
        </div>
        <div class="txt_list">
          <span class="lable font14 text_black">{{$t('用户类型')}}：</span>
          <span class="txt font14 text_black">{{ detailFrom.user_type }}</span>
        </div>
        <div class="txt_list">
          <span class="lable font14 text_black">{{$t('用户昵称')}}：</span>
          <span class="txt font14 text_black">{{ detailFrom.nick }}</span>
        </div>
        <div class="txt_list">
          <span class="lable font14 text_black">{{$t('订单类型')}}：</span>
          <span class="txt font14 text_black">
            {{
            detailFrom.order_type == 1 ? $t('充氧宝') : $t('密码线')
            }}
          </span>
        </div>
        <div class="txt_list">
          <span class="lable font14 text_black">{{$t('设备号')}}：</span>
          <span class="txt font14 text_black">{{ detailFrom.device_id }}</span>
        </div>

        <div class="txt_list">
          <span class="lable font14 text_black">{{$t('下单时间')}}：</span>
          <span class="txt font14 text_black">{{ detailFrom.start_time }}</span>
        </div>
        <div class="txt_list">
          <span class="lable font14 text_black">{{$t('使用时间')}}：</span>
          <span class="txt font14 text_black">{{ detailFrom.use_time }}</span>
        </div>
        <div class="txt_list" v-for="(item, index) in detailFrom.brokerage" :key="index">
          <span class="lable font14 text_black">{{ item.type }}：</span>
          <span class="txt font14 text_black">{{ item.name }} -- {{ item.ratio }}%{{$t('分成')}}</span>
        </div>

        <div class="txt_list">
          <span class="lable font14 text_danger">{{$t('订单金额')}}：</span>
          <span class="txt font14 text_danger">￥{{ detailFrom.amount }}</span>
        </div>
        <div class="txt_list">
          <span class="lable font14 text_danger">{{$t('订单状态')}}：</span>
          <span class="txt font14 text_danger">
            {{
            detailFrom.status_text
            }}
          </span>
        </div>
      </div>
    </el-dialog>

    <agent-list ref="agentList" @hide="closeAgentList"></agent-list>
    <merchant-list ref="merchantList" :agency_id="aid" @hide="closeMerchantList"></merchant-list>
    <refresh @click="handleRefresh"></refresh>
  </div>
</template>

<script>
import agentList from "components/agent-list/agent-list.vue";
import merchantList from "components/merchant-list/merchant-list.vue";
import { mapState } from "vuex";
import util from "@/assets/js/util.js";
export default {
  name: "order-wiredList",
  data() {
    let that = this;
    return {
      page: 1,
      page_size: 20,
      total: 0,
      order_id: "",
      member_id: "",
      qrcode: "",
      clientVal: 0,
      clientArr: [
        {
          value: 0,
          label: that.$t("选择客户端")
        },
        {
          value: 1,
          label: that.$t("微信小程序")
        },
        {
          value: 2,
          label: that.$t("支付宝小程序")
        }
      ],
      orderStatusVal: 0,
      orderStatusArr: [
        {
          value: 0,
          label: that.$t("选择订单状态")
        },
        {
          value: 1,
          label: that.$t("租借中")
        },
        {
          value: 2,
          label: that.$t("已完成")
        },
        {
          value: 3,
          label: that.$t("已撤销")
        }
      ],
      agent: "",
      aid: "",
      merchant: "",
      sid: "",
      dates: "",
      pickerOptions: {
        shortcuts: [
          {
            text: that.$t("最近一周"),
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: that.$t("最近一个月"),
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      tableData: [],
      showOrderDetail: false,
      detailFrom: {},
      radio: "1",
      pay_amount: 0,
      textarea: ""
    };
  },
  computed: {
    ...mapState(["permissions"])
  },
  components: {
    agentList,
    merchantList
  },
  mounted() {
    let arr = util.getDate('month')
    this.dates = [ arr[0] + ' 00:00:00', arr[1] + ' 23:59:59' ]
    this.getData();
  },
  methods: {
    handleRefresh() {
      let arr = util.getDate('month')
      this.dates = [ arr[0] + ' 00:00:00', arr[1] + ' 23:59:59' ]
      this.page = 1
      this.page_size = 20
      this.order_id = ''
      this.member_id = ''
      this.qrcode = ''
      this.aid = ''
      this.clientVal = 0
      this.sid = ''
      this.orderStatusVal = 0
      this.getData()
    },
    getData(type) {
      if(type == 'search') { this.page = 1 }
      util
        .$http("/order/wiredList?page=" + this.page, {
          page_size: this.page_size,
          order_no: this.order_id,
          member_id: this.member_id,
          device_code: this.qrcode,
          aid: this.aid,
          app_type: this.clientVal,
          sid: this.sid,
          status: this.orderStatusVal,
          start_time: this.dates[0] || "",
          end_time: this.dates[1] || ""
        })
        .then(res => {
          if (res.code == 1) {
            this.total = res.data.total;
            this.tableData = res.data.list;
          }
        });
    },
    clientChange(val) {
      this.clientVal = val;
    },
    orderStatusChange(val) {
      this.orderStatusVal = val;
    },
    openAgentList() {
      this.$refs.agentList.show();
    },
    closeAgentList(data) {
      this.agent = data.name;
      this.aid = data.id;
      this.merchant = "";
      this.sid = "";
    },
    clearAgent() {
      this.agent = "";
      this.aid = "";
      this.merchant = "";
      this.sid = "";
    },
    openMerchantList() {
      this.$refs.merchantList.show();
    },
    closeMerchantList(data) {
      this.merchant = data.name;
      this.sid = data.id;
    },
    clearMerchant() {
      this.merchant = "";
      this.sid = "";
    },
    handleDetail(row) {
      this.showOrderDetail = true;
      setTimeout(() => {
        util
        .$http("/order/detail", {
          order_no: row.order_no
        })
        .then(res => {
          if (res.code == 1) {
            this.detailFrom = res.data;
          }
        });
      }, 150);
    },
    handleDelete(row) {
      this.$confirm(
        this.$t("撤销订单后") +
          "," +
          this.$t("订单金额会退回用户余额") +
          "," +
          this.$t("所有代理佣金也将撤销")
      )
        .then(() => {
          util
            .$http("/order/cancelOrder", {
              order_no: row.order_no
            })
            .then(res => {
              if (res.code == 1) {
                this.$message({
                  message: res.msg,
                  type: "success"
                });
                this.getData();
              }
            });
        })
        .catch(() => {});
    },
    handleCurrentChange(val) {
      this.page = Number(val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.head_search {
  margin-bottom: 20px;
}
.head_search .ipt_box {
  width: 260px;
  margin-right: 20px;
}
.head_search .ipt_box .el-input {
  width: 200px;
}
.head_search .select_box {
  width: 280px;
  margin-right: 20px;
}
.head_search .select_box .el-select {
  width: 200px;
}
.head_search .div_box {
  display: flex;
  align-items: center;
  width: 320px;
  margin-right: 20px;
}
.head_search .div_box .content {
  width: 170px;
  height: 40px;
  padding-left: 15px;
  border: 1px solid #dcdfe6;
  border-radius: 4px 0 0 4px;
  transition: border 0.3s;
  box-sizing: border-box;
}
.head_search .div_box .content:active {
  border-color: #409eff;
}
.head_search .div_box .btn {
  width: 60px;
  height: 40px;
  border: 0;
  outline: 0;
  cursor: pointer;
  border-radius: 0 4px 4px 0;
  box-sizing: border-box;
}
.head_search .date_box{
  width: 630px;
  margin-right: 20px;
}
.head_search .date_box .tip{
  width: 150px;
  margin: 0 0 0 10px;
}
.head_search .switch_box {
  width: 151px;
  margin-right: 20px;
}
.head_search .sub_btn {
  width: 70px;
  height: 40px;
  border: 0;
  outline: 0;
  cursor: pointer;
  border-radius: 4px;
}
.head_tip {
  margin: 0 0 10px 0;
  padding-bottom: 5px;
  border-bottom: 1px solid #ebeef5;
}
.iconfont {
  font-size: 15px;
  margin-right: 5px;
}
.el-button--mini,
.el-button--mini.is-round {
  padding: 7px 10px;
}
.pagination_box {
  padding-top: 30px;
}
.pagination_box .el-pagination {
  display: flex;
  justify-content: flex-end;
}
.order_detail {
  padding: 0 10px;
  margin-top: -15px;
}
.order_detail .txt_list {
  margin-bottom: 15px;
}
.order_detail .ipt_list .el-input {
  width: 150px;
  margin-right: 15px;
}
.order_detail .el-input .el-input__inner {
  height: 36px;
  line-height: 36px;
}
.order_detail .el-button {
  margin-top: 30px;
}
.end_order {
  padding-left: 50px;
}
.end_order .txt_list {
  margin-bottom: 20px;
}
.end_order .txt_list .lable {
  display: inline-block;
  width: 80px;
}
.end_order .txt_list ul {
  padding: 0;
  margin: 0;
}
.end_order .txt_list .li {
  margin-bottom: 5px;
  list-style: none;
}
.end_order .radio_list {
  margin-bottom: 20px;
}
.end_order .radio_list .lable {
  display: inline-block;
  width: 80px;
}
.end_order .radio_list .txt {
  margin-left: 20px;
  cursor: pointer;
}
.end_order .ipt_list {
  margin-bottom: 20px;
}
.end_order .ipt_list .lable {
  display: inline-block;
  width: 80px;
}
.end_order .ipt_list .el-input {
  width: 150px;
  margin-right: 15px;
}
.end_order .el-input .el-input__inner {
  height: 36px;
  line-height: 36px;
}
.end_order .textarea_list .lable {
  display: inline-block;
  width: 80px;
}
.end_order .textarea_list .el-textarea {
  width: 360px;
}
.end_order .textarea_list .el-textarea__inner {
  min-height: 90px !important;
}
.end_order .el-button {
  margin-top: 30px;
  margin-left: 80px;
}
</style>
